﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Ccit.Utils;
using Ccit.ClassLibrary;

namespace Ccit.SysPlat.Purview
{
    /// <summary>
    /// Description: 系统平台权限菜单 -> 角色管理业务层
    /// Author: tim 2010-03-04
    /// </summary>
    public class RoleBLL
    {
        #region initialize 

        IRole _roleProcessor = null;

        public RoleBLL()
        {
            _roleProcessor = Factory.CreateObject(MsRole.assemblyName, MsRole.className, ImportComponentType.DotNetDll, null) as IRole;
        }

        public void Release()
        {
            _roleProcessor = null;
        }
        #endregion

        /// <summary>
        /// 获取角色集合
        /// </summary>
        /// <param name="organId">机构Id</param>
        /// <param name="sysRole">是否获取系统角色(获取角色类型 普通 / 系统角色)</param>
        /// <param name="curUserId">当前登录用户Id
        ///     只能修改由本人创建的角色
        /// </param>
        /// <returns></returns>
        public List<MsRole> GetRoleList(string organId, eTrueFalse sysRole, string curUserId)
        {
            return _roleProcessor.GetRoleList(organId, sysRole, curUserId);
        }


        /// <summary>
        /// 获取角色消息对象
        /// </summary>
        /// <param name="roleId">角色Id</param>
        /// <returns></returns>
        public MsRole GetRoleInfo(string roleId)
        {
            if (string.IsNullOrEmpty(roleId)) return null;

            return _roleProcessor.GetRoleInfo(roleId);
        }

        /// <summary>
        /// 获取管理菜单数据
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="enUserType">用户类型枚举</param>
        /// <param name="organId">机构Id</param>
        /// <returns></returns>
        public List<MsMenu> GetAdminMenus(string userId, eUserType enUserType,string organId)
        {
            if (enUserType == eUserType.ADMINISTRATOR) return _roleProcessor.GetSysAdminMenus(organId);
            else return _roleProcessor.GetModAdminMenus(userId,organId);
        }

        /// <summary>
        /// 校验本机构下的角色是否同名
        /// (但普通角色与系统角色可以重名)
        /// </summary>
        /// <param name="roleId">部门Id</param>
        /// <param name="roleName">部门名称</param>
        /// <param name="organId">机构Id</param>
        /// <param name="sysRole">当前创建的是否系统角色(角色类型:【普通 / 系统】)</param>
        /// <returns></returns>
        public List<string> IsExistRoleName(string roleId, string roleName, string organId, eTrueFalse sysRole)
        {
            if (string.IsNullOrEmpty(roleName)) return new List<string> { "-999", "操作失败:缺少必要参数!" };
            bool bExist = _roleProcessor.IsExistSameName(roleId, roleName, organId, sysRole);
            if (bExist) return new List<string> { "-990", "已存在同名角色!" };
            else return new List<string> { "1", "角色名称合法!" };
        }


        /// <summary>
        /// 保存角色
        /// </summary>
        /// <param name="msRole">角色信息消息对象</param>
        /// <returns></returns>
        public List<string> Save(MsRole msRole)
        {
            if (null == msRole) return new List<string> { "-999", "操作失败:缺少必要参数!" };

            List<string> lstResult = _roleProcessor.Save(msRole);
            if(Int32.Parse(lstResult[0]) > 0)
            {
                //需要更新当前用户的操作菜单权限
                MsLoginUser loginUser = HttpContext.Current.Session["LogUserInfo"] as MsLoginUser;
                IPurview _prvProcessor = Factory.CreateObject(MsPurview.assemblyName,MsPurview.className,
                                                                        ImportComponentType.DotNetDll,null) as IPurview;
                loginUser.operMenuIds = _prvProcessor.GetUserPrvMenuIds(loginUser.userId, loginUser.userType, loginUser.organId);
                HttpContext.Current.Session["LogUserInfo"] = loginUser;
                _prvProcessor = null;
            }
            return lstResult;
        }


        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="roleId">角色Id</param>
        /// <returns></returns>
        public List<string> Delete(string roleId)
        {
            if (string.IsNullOrEmpty(roleId)) return new List<string> { "-999", "操作失败:缺少必要参数!" };
            return _roleProcessor.Delete(roleId);
        }
    }
}
